﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using QLTT.Models;
using QLTT.Properties;
using QLTT.Commands;
using System.Text.RegularExpressions;

namespace QLTT.ViewModels
{
    class ScheduleViewModel:WorkspaceViewModel, System.ComponentModel.IDataErrorInfo
    {
        private Schedule _schedule;
        private icanDataContext _dataContext;
        private DelegateCommand _saveCommand;
        private DelegateCommand _editCommand;
        private DelegateCommand _cancelCommand;
        private ClassTimeViewModel _ctVM;
        private ClassViewModel _cVM;
                
        private bool _isSelected;
        
        public ScheduleViewModel(Schedule schedule, icanDataContext dataContext)
        {
            _dataContext = dataContext;
            _schedule = schedule;
            ClassTime classTime = _dataContext.ClassTimes.FirstOrDefault(ct => ct.ID == schedule.ClassTime);
            _ctVM = new ClassTimeViewModel(_dataContext, classTime);
            _cVM = new ClassViewModel(_dataContext, _schedule.Class1);
            
        }
        public ScheduleViewModel(ClassViewModel cVM, ClassTimeViewModel ctVM, short dayOfWeek, icanDataContext dataContext)
        {
            _schedule = new Schedule();
            this.Class = cVM;
            this.ClassTime = ctVM;
            this.DayOfWeek = dayOfWeek;
            _dataContext = dataContext;
        }
        public string GetHashCode()
        {
            return string.Format("{0}-{1}-{2}", _schedule.Class, _schedule.ClassTime, _schedule.DayOfWeek);
        }
        public string GetUniqueCode()
        {
            return string.Format("{0}-{1}-{2}-{3}", _schedule.Class, _schedule.ClassTime, _schedule.DayOfWeek, _schedule.Teacher);
        }
        public ICommand SaveCommand
        {
            get
            {
                if (_saveCommand == null)
                {
                    _saveCommand = new DelegateCommand(Save);
                }
                return _saveCommand;
            }
        }
        public ICommand EditCommand
        {
            get
            {
                if (_editCommand == null)
                {
                    _editCommand = new DelegateCommand(Edit);
                }
                return _editCommand;
            }
        }
        public ICommand CancelCommand
        {
            get
            {
                if (_cancelCommand == null)
                {
                    _cancelCommand = new DelegateCommand(Cancel);
                }
                return _cancelCommand;
            }
        }

        public void Save()
        {
            if (_dataContext == null || _schedule == null)
                return;
            try
            {
                bool isInsert = _schedule.ID == 0;
                if (isInsert)
                {
                    _dataContext.Schedules.InsertOnSubmit(_schedule);
                }
                _dataContext.SubmitChanges();
            }
            catch (Exception ex)
            {
                System.Diagnostics.EventLog.WriteEntry("Application", ex.ToString(), System.Diagnostics.EventLogEntryType.Error);
            }
            
        }
       
        private void Edit()
        {
            if (_dataContext == null || _schedule == null)
                return;
            
            Mediator.NotifyColleagues<WorkspaceViewModel>(Messages.WorkspaceAdd, this);
        }
        private void Cancel()
        {
            if(_schedule.ID > 0)
                _dataContext.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, _schedule);
            Mediator.NotifyColleagues<WorkspaceViewModel>(Messages.WorkspaceRemove, this);
        }
        public void Delete()
        {
            if (_dataContext == null || _schedule == null)
                return;
            try
            {
                _dataContext.Schedules.DeleteOnSubmit(_schedule);
                _dataContext.SubmitChanges();
            }
            catch (Exception ex)
            {
                System.Diagnostics.EventLog.WriteEntry("Application", ex.ToString(), System.Diagnostics.EventLogEntryType.Error);
            }
        }
        
        public long ID
        {
            get { return _schedule.ID; }
        }

        public ClassViewModel Class
        {
            get { return _cVM; }
            set
            {
                _schedule.Class = value.ID;
                _cVM = value;
                base.OnPropertyChanged("Class");
            }
        }
        public ClassTimeViewModel ClassTime
        {
            get { return _ctVM; }
            set
            {
                _schedule.ClassTime = value.ID;
                _ctVM = value;
                base.OnPropertyChanged("ClassTime");
            }
        }
        public short DayOfWeek
        {
            get { return _schedule.DayOfWeek; }
            set
            {
                if (_schedule.DayOfWeek == value) return;
                _schedule.DayOfWeek = value;
                base.OnPropertyChanged("DayOfWeek");
            }
        }
        public long Teacher
        {
            get { return _schedule.Teacher; }
            set
            {
                if (_schedule.Teacher == value) return;
                _schedule.Teacher = value;
                base.OnPropertyChanged("Teacher");
            }
        }
        /* List Teacher for Teacher Combobox*/
        public List<TeacherViewModel> Teachers
        {
            get
            {
                List<TeacherViewModel> lstTeacher = _dataContext.Teachers.Select(t => new TeacherViewModel(_dataContext, t)).ToList();
                lstTeacher.Insert(0, new TeacherViewModel(_dataContext));
                return lstTeacher;
            }
        }
        public string FullName
        {
            get
            {
                return _ctVM.FullName;
            }
        }
        
        public bool IsSelected
        {
            get { return _isSelected; }
            set
            {
                if (value == _isSelected)
                    return;

                _isSelected = value;

                base.OnPropertyChanged("IsSelected");
            }
        }
        #region IDataErrorInfo Members

        public string Error
        {
            get { return null; }
        }

        public string this[string property]
        {
            get
            {
                string msg = null;
                Regex rxValidate;
                switch (property)
                {
                    default:
                        throw new ArgumentException(
                            Resources.ValidationDefaultException_Error + property);
                }
                return msg;
            }
        }

        #endregion // IDataErrorInfo Members

    }
}

